package top.xierongsheng.code.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import top.xierongsheng.code.CacheVariable;
import top.xierongsheng.code.R;
import top.xierongsheng.code.redis.RedisService;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;

import java.util.Objects;

@Slf4j
@Aspect
@Component
public class RequestAuthorizeAspect {

    @Resource
    private RedisService redisService;

    //定义切入点(使用范围)
    @Pointcut("execution(* top.xierongsheng.*.controller..*.*(..))")
    public void requestAuthorizePointCut() {
    }

    @Around("requestAuthorizePointCut()")
    public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();
        String url = request.getRequestURI();
        String userId = request.getHeader("userId");
//        if (!url.equals("/system/login") && !url.equals("/member/wxLogin") && !url.equals("/system/test")) {
//            Object obj = redisService.get(CacheVariable.USER + userId);
//            if (obj == null) {
//                return R.timeout();
//            }
//        }
        return proceedingJoinPoint.proceed();
    }

}
